package cate.game.chat;

import cate.common.table.d.GDChat;
import cate.game.GameBody;
import cate.game.framework.ThreadShare;
import cate.game.role.Role;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class ChatFacade {
    /**
     * 作为【系统发送者】发给所有人的系统频道
     */
    public static void sendSystemAsSystem(Role role, GameBody game, int contentId, List<ChatContentParam> params, boolean noticeSelf) {
        if (noticeSelf) {
            sendSelfSystem(role, GDChat.Channel.SYSTEM, contentId, params);
            return;
        }
        sendAsSystem(game, null, GDChat.Channel.SYSTEM, contentId, params);
    }

    public static void sendEliteRaceAsSystem(GameBody game, int contentId) {
        sendAsSystem(game, null, GDChat.Channel.ELITE_RACE, contentId, null);
    }

    public static void sendAsSystem(GameBody game, ChatFilter filter, byte channel, int contentId, List<ChatContentParam> params) {
        ChatPublicItem item = new ChatPublicItem(ChatSenderInfo.getSystemSender(), channel, contentId, null, params);
        game.chat.sendPublic(item, filter);
    }

    public static void sendSelfSystem(Role role, byte channel, int contentId, List<ChatContentParam> params) {
        if(role == null){
            return;
        }
        ChatPublicItem item = new ChatPublicItem(ChatSenderInfo.getSystemSender(), channel, contentId, null, params);
        role.getGame().chat.sendSelfPublic(role, item);
    }

    /**
     * 作为【系统发送者】发送给所有人的世界频道
     */
    public static void sendWorldAsSystem(GameBody game, int contentId, List<ChatContentParam> params) {
        sendAsSystem(game, null, GDChat.Channel.WORLD, contentId, params);
    }

    /**
     * 作为【系统发送者】发送给所有人的仙盟频道
     */
    public static void sendGuildAsSystem(GameBody game, int contentId, List<ChatContentParam> params) {
        sendAsSystem(game, null, GDChat.Channel.GUILD, contentId, params);
    }

    /**
     * 作为【系统发送者(成员)】发送给相同仙盟成员的仙盟频道
     */
    public static void sendGuildAsSystemMember(GameBody game,
                                               String guildIdHex, int contentId, String content, List<ChatContentParam> params) {
        ChatPublicItem item =
                new ChatPublicItem(
                        ChatSenderInfo.getGuildSender(guildIdHex),
                        GDChat.Channel.GUILD,
                        contentId,
                        content,
                        params);
        ChatFilter filter = new ChatFilter();
        filter.guildSameRequired = true;
        game.chat.sendPublic(item, filter);
    }


    /**
     * 作为【成员】发送给相同仙盟成员的仙盟频道只发contentId
     */
    public static void sendGuildAsMemberId(Role sender, int contentId) {
        sendPublicFromClient(sender, GDChat.Channel.GUILD, contentId, null, null);
    }

    /**
     * 作为【成员】发送给相同仙盟成员的仙盟频道只发contentId
     */
    public static void sendGuildAsMemberId(Role sender, int contentId, List<ChatContentParam> params) {
        sendPublicFromClient(sender, GDChat.Channel.GUILD, contentId, null, params);
    }

    /** 从客户端发来请求发送到公共频道 */
    public static void sendPublicFromClient(Role sender, byte channel, int contentId, String content, List<ChatContentParam> params) {
        ChatPublicItem item = new ChatPublicItem(sender, channel, contentId, content, params);
        ChatFilter filter = new ChatFilter();
        if (channel == GDChat.Channel.GUILD) {
            if(StringUtils.isBlank(sender.getRes().guild.uid)){
                return;
            }
            filter.guildSameRequired = true;
            filter.guildUid = sender.getRes().guild.uid;
        }
        filter.filterBlackList = true;
        sender.getGame().chat.sendPublic(item, filter);
        sender.getTrace().addGuildChat();
    }

    /**
     * 作为【系统发送者】发送给所有人跨服频道
     */
    public static void sendCrossAsSystem(GameBody game, int contentId, List<ChatContentParam> params) {
        ChatPublicItem item = new ChatPublicItem(ChatSenderInfo.getSystemSender(), GDChat.Channel.CROSS, contentId, null, params);
//        SA.mq.cross.sendAll(MqCrossContentType.CHAT_PUBLIC_REQ, item);
    }
}
